<template>
  <div>
    <el-dialog
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isShow"
      center
      :before-close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          prop="title"
          label="菜单名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">
          <el-select
            @change="changePid"
            v-model="form.pid"
            placeholder="请选择上级菜单"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menulist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="form.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="form.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item
          v-if="form.type == 1"
          label="菜单图标"
          :label-width="formLabelWidth"
        >
          <el-select v-model="form.icon">
            <el-option
              v-for="item in iconlist"
              :key="item"
              :value="item"
              :label="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else :label-width="formLabelWidth" label="菜单地址">
          <el-select v-model="form.url">
            <el-option
              v-for="item in indexRoutes"
              :key="item.path"
              :value="item.path"
              :label="item.meta.title"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#409EFF"
            inactive-color="#DCDFE6"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { menuAdd, menuEdit, menuInfo } from "@/request/api";
import { mapActions, mapGetters } from "vuex";
import { indexRoutes } from "@/router";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      form: {
        pid: 0, //上级分类编号  注意：顶级菜单-0
        title: "", //菜单名称
        icon: "", // 图标
        type: 1, //类型 1目录2菜单
        url: "", //菜单地址
        status: 1, //状态   1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 7, message: "长度在 2 到 7 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },
      indexRoutes,
      iconlist: [
        "el-icon-platform-eleme",
        "el-icon-delete-solid",
        "el-icon-s-tools",
        "el-icon-warning",
        "el-icon-message-solid",
        "el-icon-attract",
        "el-icon-s-data",
        "el-icon-s-goods",
      ],
    };
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenuList",
    }),
  },
  methods: {
    changePid() {
      this.form.type = this.form.pid == 0 ? 1 : 2;
    },
    lookup(id) {
      menuInfo({
        id,
      }).then((res) => {
        console.log(res, "一条数据");
        this.form = res.list;
        this.form.id = id;
      });
    },
    ...mapActions({
      getMenu: "menu/changeMenuAction",
    }),
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      (this.form = {
        pid: 0,
        title: "",
        icon: "",
        type: 1,
        url: "",
        status: 1,
      }),
        this.$refs.form.resetFields();
    },
    comfirm() {
      this.$refs.form.validate((flag) => {
        console.log(flag);
        if (flag) {
          // console.log(this.form);
          if (this.isAdd) {
            menuAdd(this.form).then((res) => {
              console.log(res, "菜单添加");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getMenu();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            menuEdit(this.form).then((res) => {
              console.log(res, "菜单编辑");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getMenu();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          alert("验证失败");
        }
      });
    },
  },
};
</script>

<style lang="less"scoped>
</style>
